package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("mobile/order")
public class OrderController {

    @DubboReference
    private SmsService smsServic;
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private SetmealService setmealService;

    /**
     * @Author: 15041
     * @Date: 16:52 2021/1/12
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 预约提交
     */
    @PostMapping("submit")
    public Result submit(@RequestBody OrderSubmitParam param) {
        log.info("预约提交-传递参数：orderSubmitParam：{}", param);

        //验证验证码或手机号是否存在
        if (StringUtils.isEmpty(param.getValidateCode()) || StringUtils.isEmpty(param.getTelephone())){
            return new Result(false,MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //根据验证码类型/手机号/验证码 来验证验证码是否正确
        boolean flagCode = smsServic.checkValidateCode("ORDER",param.getTelephone(),param.getValidateCode());
        //保存或更新会员信息
        Member member = new Member();
        //设置预约人手机号
        member.setPhoneNumber(param.getTelephone());
        //设置预约人身份证号
        member.setIdCard(param.getIdCard());
        //设置预约人性别
        member.setSex(param.getSex());
        //设置预约人姓名
        member.setName(param.getName());
        //根据前端传过来的信息更新或新增预约人
        Member newMember = memberService.insertOrUpdate(member);
        log.info("新增或更新预约人信息-结果是：{}",newMember);
        //保存订单
        //创建一个新的预约信息对象
        Order order = new Order();
        order.setOrderDate(param.getOrderDate()); //添加预约时间
        order.setOrderType(Order.ORDERTYPE_WEIXIN); //添加预约方式
        order.setOrderStatus(Order.ORDERSTATUS_NO); //添加预约状态
        order.setSetmealId(param.getSetMealId());
        order.setMemberId(newMember.getId()); //添加预约人id
        //调用新增预约信息方法
        Order newOrder = orderService.add(order);
        return new Result(true, MessageConst.ORDER_SUCCESS,newOrder);
    }

    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("预约查询：根据id查询id：{}",id);
        Order order = orderService.findById(id);
        //根据预约人id 获取预约人名称
        Member member = memberService.findById(order.getMemberId());
        //根据套餐id 获取套餐名称
        Setmeal setmeal = setmealService.findById(order.getSetmealId());
        Map resultMap = new HashMap<>();
        resultMap.put("member",member.getName());
        resultMap.put("setmeal",setmeal.getName());
        resultMap.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        resultMap.put("orderType",order.getOrderType());
        log.info("预约成功展示信息：resultMap:{}",resultMap);
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,resultMap);
    }

}
